# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'dialog_add_table.ui'
##
## Created by: Qt User Interface Compiler version 6.3.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
import pymysql
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QAbstractButton, QApplication, QComboBox, QDialog,
                               QDialogButtonBox, QHBoxLayout, QLabel, QLineEdit,
                               QSizePolicy, QVBoxLayout, QWidget, QMessageBox, QListWidget, QListWidgetItem)

from global_variables import GlobalVariables


class DialogAddTable(QDialog):
    def __init__(self, parent, app):
        super(DialogAddTable, self).__init__(parent)
        self.__list_field_data__ = []
        self.__app__ = app
        self.__mysqldb__ = GlobalVariables.DATABASE_OBJ
        self.setupUi(self)
        self.init()

    def init(self):
        self.buttonBox.accepted.disconnect(self.accept)
        self.buttonBox.accepted.connect(self.button_box_accepted)

        # 数据表引擎添加选项
        self.table_engine.addItems(GlobalVariables.LIST_DATABASE_ENGINE)
        for engine in GlobalVariables.LIST_DATABASE_ENGINE:
            if engine.upper() == 'INNODB':
                self.table_engine.setCurrentText(engine)
        # 数据表编码与排序集添加选项
        list_encoding = set()
        for collection, encoding in GlobalVariables.MAP_COLLECTION_CHARSET.items():
            list_encoding.add(encoding)
        list_encoding = list(list_encoding)
        list_encoding.sort()
        self.table_encoding.addItems(list_encoding)
        # 添加事件，当字符集变化时，排序集发生变化
        self.table_encoding.currentTextChanged.connect(self.table_encoding_current_text_changed)
        # 设置默认
        self.table_encoding.setCurrentText("utf8mb4")

    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(364, 262)
        Dialog.setWindowTitle("添加数据表")
        self.verticalLayout_2 = QVBoxLayout(Dialog)
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.widget_5 = QWidget(Dialog)
        self.widget_5.setObjectName(u"widget_5")
        self.verticalLayout = QVBoxLayout(self.widget_5)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.widget = QWidget(self.widget_5)
        self.widget.setObjectName(u"widget")
        self.horizontalLayout = QHBoxLayout(self.widget)
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.label = QLabel(self.widget)
        self.label.setObjectName(u"label")

        self.horizontalLayout.addWidget(self.label)

        self.table_name = QLineEdit(self.widget)
        self.table_name.setObjectName(u"table_name")

        self.horizontalLayout.addWidget(self.table_name)


        self.verticalLayout.addWidget(self.widget)

        self.widget_2 = QWidget(self.widget_5)
        self.widget_2.setObjectName(u"widget_2")
        self.horizontalLayout_2 = QHBoxLayout(self.widget_2)
        self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
        self.label_2 = QLabel(self.widget_2)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setMaximumSize(QSize(72, 16777215))

        self.horizontalLayout_2.addWidget(self.label_2)

        self.table_encoding = QComboBox(self.widget_2)
        self.table_encoding.setObjectName(u"table_encoding")

        self.horizontalLayout_2.addWidget(self.table_encoding)


        self.verticalLayout.addWidget(self.widget_2)

        self.widget_3 = QWidget(self.widget_5)
        self.widget_3.setObjectName(u"widget_3")
        self.horizontalLayout_4 = QHBoxLayout(self.widget_3)
        self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
        self.label_3 = QLabel(self.widget_3)
        self.label_3.setObjectName(u"label_3")
        self.label_3.setMaximumSize(QSize(72, 16777215))

        self.horizontalLayout_4.addWidget(self.label_3)

        self.table_collections = QComboBox(self.widget_3)
        self.table_collections.setObjectName(u"table_collections")

        self.horizontalLayout_4.addWidget(self.table_collections)


        self.verticalLayout.addWidget(self.widget_3)

        self.widget_4 = QWidget(self.widget_5)
        self.widget_4.setObjectName(u"widget_4")
        self.horizontalLayout_3 = QHBoxLayout(self.widget_4)
        self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
        self.label_4 = QLabel(self.widget_4)
        self.label_4.setObjectName(u"label_4")
        self.label_4.setMaximumSize(QSize(72, 16777215))

        self.horizontalLayout_3.addWidget(self.label_4)

        self.table_engine = QComboBox(self.widget_4)
        self.table_engine.setObjectName(u"table_engine")

        self.horizontalLayout_3.addWidget(self.table_engine)


        self.verticalLayout.addWidget(self.widget_4)


        self.verticalLayout_2.addWidget(self.widget_5)

        self.buttonBox = QDialogButtonBox(Dialog)
        self.buttonBox.setObjectName(u"buttonBox")
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)

        self.verticalLayout_2.addWidget(self.buttonBox)


        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        self.label.setText(QCoreApplication.translate("Dialog", u"\u6570\u636e\u8868\u540d\u79f0\uff1a", None))
        self.label_2.setText(QCoreApplication.translate("Dialog", u"\u6570\u636e\u8868\u7f16\u7801\uff1a", None))
        self.label_3.setText(QCoreApplication.translate("Dialog", u"\u6570\u636e\u8868\u6392\u5e8f\uff1a", None))
        self.label_4.setText(QCoreApplication.translate("Dialog", u"\u6570\u636e\u8868\u5f15\u64ce\uff1a", None))
    # retranslateUi

    def table_encoding_current_text_changed(self, text):
        list_collections = set()
        for collection, encoding in GlobalVariables.MAP_COLLECTION_CHARSET.items():
            if text == encoding:
                list_collections.add(collection)
        list_collections = list(list_collections)
        list_collections.sort()
        self.table_collections.clear()
        self.table_collections.addItems(list_collections)
        if text == "utf8mb4":
            self.table_collections.setCurrentText("utf8mb4_general_ci")

    def button_box_accepted(self):
        if len(self.table_name.text()) == 0:
            QMessageBox.critical(self, '添加数据表错误', "请输入表名称")
            return
        self.accept()
        table_name = self.table_name.text()
        table_encoding = self.table_encoding.currentText()
        table_collection = self.table_collections.currentText()
        table_engine = self.table_engine.currentText()
        try:
            self.__mysqldb__.create_table(
                table_name=table_name,
                table_encoding=table_encoding,
                table_collection=table_collection,
                table_engine=table_engine,
            )
        except (pymysql.err.DataError, pymysql.err.OperationalError) as e:
            code, msg = e.args
            QMessageBox.critical(self, '添加数据表错误', str(code) + "\t" + msg)
            raise e
        self.__app__.table_list.refresh_items()
        self.__app__.ui.table_list: QListWidget
        for row in range(0, self.__app__.ui.table_list.count()):
            item: QListWidgetItem = self.__app__.ui.table_list.item(row)
            if item.text() == "table_name":
                self.__app__.ui.table_list.setCurrentItem(item)
